home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / awt / image / direct~1.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  6.8 KB  |  224 lines

  1. /*
  2.  * @(#)DirectColorModel.java    1.12 95/12/14 Jim Graham
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.awt.image;
  21.  
  22. import java.awt.AWTException;
  23.  
  24. /**
  25.  * A ColorModel class that specifies a translation from pixel values
  26.  * to alpha, red, green, and blue color components for pixels which
  27.  * have the color components embedded directly in the bits of the
  28.  * pixel itself.  This color model is similar to an X11 TrueColor
  29.  * visual.
  30.  
  31.  * <p>Many of the methods in this class are final. This is because the
  32.  * underlying native graphics code makes  assumptions about the layout
  33.  * and operation of this class  and those assumptions are reflected in
  34.  * the implementations of the methods here that are marked final.  You
  35.  * can subclass this class  for other reaons,  but you cannot override
  36.  * or modify the behaviour of those methods.
  37.  *
  38.  * @see ColorModel
  39.  *
  40.  * @version    1.12 12/14/95
  41.  * @author     Jim Graham
  42.  */
  43. public class DirectColorModel extends ColorModel {
  44.     private int red_mask;
  45.     private int green_mask;
  46.     private int blue_mask;
  47.     private int alpha_mask;
  48.     private int red_offset;
  49.     private int green_offset;
  50.     private int blue_offset;
  51.     private int alpha_offset;
  52.     private int red_bits;
  53.     private int green_bits;
  54.     private int blue_bits;
  55.     private int alpha_bits;
  56.  
  57.     /**
  58.      * Constructs a DirectColorModel from the given masks specifying
  59.      * which bits in the pixel contain the red, green and blue color
  60.      * components.  Pixels described by this color model will all
  61.      * have alpha components of 255 (fully opaque).  All of the bits
  62.      * in each mask must be contiguous and fit in the specified number
  63.      * of least significant bits of the integer.
  64.      */
  65.     public DirectColorModel(int bits,
  66.                 int rmask, int gmask, int bmask) {
  67.     this(bits, rmask, gmask, bmask, 0);
  68.     }
  69.  
  70.     /**
  71.      * Constructs a DirectColorModel from the given masks specifying
  72.      * which bits in the pixel contain the alhpa, red, green and blue
  73.      * color components.  All of the bits in each mask must be contiguous
  74.      * and fit in the specified number of least significant bits of the
  75.      * integer.
  76.      */
  77.     public DirectColorModel(int bits,
  78.                 int rmask, int gmask, int bmask, int amask) {
  79.     super(bits);
  80.     red_mask = rmask;
  81.     green_mask = gmask;
  82.     blue_mask = bmask;
  83.     alpha_mask = amask;
  84.     CalculateOffsets();
  85.     }
  86.  
  87.     /**
  88.      * Returns the mask indicating which bits in a pixel contain the red
  89.      * color component.
  90.      */
  91.     final public int getRedMask() {
  92.     return red_mask;
  93.     }
  94.  
  95.     /**
  96.      * Returns the mask indicating which bits in a pixel contain the green
  97.      * color component.
  98.      */
  99.     final public int getGreenMask() {
  100.     return green_mask;
  101.     }
  102.  
  103.     /**
  104.      * Returns the mask indicating which bits in a pixel contain the blue
  105.      * color component.
  106.      */
  107.     final public int getBlueMask() {
  108.     return blue_mask;
  109.     }
  110.  
  111.     /**
  112.      * Returns the mask indicating which bits in a pixel contain the alpha
  113.      * transparency component.
  114.      */
  115.     final public int getAlphaMask() {
  116.     return alpha_mask;
  117.     }
  118.  
  119.     private int accum_mask = 0;
  120.  
  121.     /*
  122.      * A utility function to decompose a single mask and verify that it
  123.      * fits in the specified pixel size, and that it does not overlap any
  124.      * other color component.  The values necessary to decompose and
  125.      * manipulate pixels are calculated as a side effect.
  126.      */
  127.     private void DecomposeMask(int mask, String componentName, int values[]) {
  128.     if ((mask & accum_mask) != 0) {
  129.         throw new IllegalArgumentException(componentName + " mask bits not unique");
  130.     }
  131.     int off = 0;
  132.     int count = 0;
  133.     if (mask != 0) {
  134.         while ((mask & 1) == 0) {
  135.         mask >>>= 1;
  136.         off++;
  137.         }
  138.         while ((mask & 1) == 1) {
  139.         mask >>>= 1;
  140.         count++;
  141.         }
  142.     }
  143.     if (mask != 0) {
  144.         throw new IllegalArgumentException(componentName + " mask bits not contiguous");
  145.     }
  146.     if (off + count > pixel_bits) {
  147.         throw new IllegalArgumentException(componentName + " mask overflows pixel");
  148.     }
  149.     values[0] = off;
  150.     values[1] = count;
  151.     }
  152.  
  153.     /*
  154.      * A utility function to verify all of the masks and to store
  155.      * the auxilliary values needed to manipulate the pixels.
  156.      */
  157.     private void CalculateOffsets() {
  158.     int values[] = new int[2];
  159.     DecomposeMask(red_mask, "red", values);
  160.     red_offset = values[0];
  161.     red_bits = values[1];
  162.     DecomposeMask(green_mask, "green", values);
  163.     green_offset = values[0];
  164.     green_bits = values[1];
  165.     DecomposeMask(blue_mask, "blue", values);
  166.     blue_offset = values[0];
  167.     blue_bits = values[1];
  168.     DecomposeMask(alpha_mask, "alpha", values);
  169.     alpha_offset = values[0];
  170.     alpha_bits = values[1];
  171.     }
  172.  
  173.     /**
  174.      * Returns the red color compoment for the specified pixel in the
  175.      * range 0-255.
  176.      */
  177.     final public int getRed(int pixel) {
  178.     if (red_mask == 0) return 0;
  179.     int r = ((pixel & red_mask) >>> red_offset);
  180.     return (red_bits == 8) ? r : (r * 255 / ((1 << red_bits) - 1));
  181.     }
  182.  
  183.     /**
  184.      * Returns the green color compoment for the specified pixel in the
  185.      * range 0-255.
  186.      */
  187.     final public int getGreen(int pixel) {
  188.     if (green_mask == 0) return 0;
  189.     int g = ((pixel & green_mask) >>> green_offset);
  190.     return (green_bits == 8) ? g : (g * 255 / ((1 << green_bits) - 1));
  191.     }
  192.  
  193.     /**
  194.      * Returns the blue color compoment for the specified pixel in the
  195.      * range 0-255.
  196.      */
  197.     final public int getBlue(int pixel) {
  198.     if (blue_mask == 0) return 0;
  199.     int b = ((pixel & blue_mask) >>> blue_offset);
  200.     return (blue_bits == 8) ? b : (b * 255 / ((1 << blue_bits) - 1));
  201.     }
  202.  
  203.     /**
  204.      * Return the alpha transparency value for the specified pixel in the
  205.      * range 0-255.
  206.      */
  207.     final public int getAlpha(int pixel) {
  208.     if (alpha_mask == 0) return 255;
  209.     int a = ((pixel & alpha_mask) >>> alpha_offset);
  210.     return (alpha_bits == 8) ? a : (a * 255 / ((1 << alpha_bits) - 1));
  211.     }
  212.  
  213.     /**
  214.      * Returns the color of the pixel in the default RGB color model.
  215.      * @see ColorModel#getRGBdefault
  216.      */
  217.     final public int getRGB(int pixel) {
  218.     return (getAlpha(pixel) << 24)
  219.         | (getRed(pixel) << 16)
  220.         | (getGreen(pixel) << 8)
  221.         | (getBlue(pixel) << 0);
  222.     }
  223. }
  224.